package com.gk.sparsearray

import java.io.File

import scala.collection.mutable.ArrayBuffer
import scala.jdk.CollectionConverters._

/**
 *
 * @Author poison
 * @Date 2020/10/28 10:52
 */
object ScalaTest2 {

  def main(args: Array[String]): Unit = {
    // 设置棋盘的大小
    val rowSize = 11
    val cowSize = 11

    //import java.net.URL
    //val url = this.getClass.getClassLoader.getResources(File.separator+"txt"+File.separator+"map.txt")


    // 将棋盘信息读入数组中
    val sparseArray: ArrayBuffer[Node] = readFromFile(rowSize, cowSize, "D:\\zhichengtaihe\\code\\workspace-scala\\scala-test1\\txt\\map.txt")

    for(node<-sparseArray){
      printf("%d\t%d\t%d",node.row,node.col,node.value)
      println()
    }

  }

  def readFromFile(rowSize:Int,colSize:Int,FilePath:String):  ArrayBuffer[Node] = {

    // 导入Scala的IO包
    import scala.io.Source

//    Source.fromFile(FilePath, "UTF-8").getLines().foreach(
//      line=>{
//        val cols: Array[String] = line.split(" ")
//      }
//    )

    val sparseArray = ArrayBuffer[Node]()

    val strIte: Iterator[String] = Source.fromFile(FilePath, "UTF-8").getLines()
    val array: Array[String] = strIte.toArray

    for(row<-0 until array.length){
      val cols: Array[String] = array(row).split(" ")
      for(col<-0 until cols.length){
        if(cols(col).toInt!=0){
          val node = new Node(row,col,cols(col).toInt)
          sparseArray.append(node)
        }
      }

    }
    sparseArray
  }
    //val sparseArray = Array.ofDim[Int](rowSize,colSize)

}
